<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%cms_special_channel}}".
 *
 * @property int $id
 * @property int $special_id
 * @property int $parent_id
 * @property string $name
 * @property string|null $thumb
 * @property string|null $description
 * @property string|null $format
 * @property int $max_level
 * @property int $listorder
 * @property int $reserved
 * @property int $navbar
 * @property int $map_id
 * @property string|null $map_data
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class CmsSpecialChannel1 extends \common\db\ActiveRecord {

    /**
     * 文章状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * 文章格式
     */
    const FORMAT_DEFAULT = 'default';
    const FORMAT_GALLERY = 'gallery';
    const FORMAT_VIDEO = 'video';
    const FORMAT_AUDIO = 'audio';
    const FORMAT_CALENDAR = 'calendar';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_special_channel}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['special_id', 'parent_id', 'listorder', 'recommend_at', 'reserved', 'navbar', 'map_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'required'],
            [['name', 'abbr', 'template', 'format'], 'string', 'max' => 50],
            [['thumb', 'description', 'link', 'map_data'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'special_id' => 'Special ID',
            'parent_id' => 'Parent ID',
            'name' => 'Name',
            'abbr' => 'Abbr',
            'thumb' => 'Thumb',
            'description' => 'Description',
            'template' => 'Template',
            'format' => 'format',
            'link' => 'Link',
            'listorder' => 'Listorder',
            'recommend_at' => 'Recommend At',
            'reserved' => 'Reserved',
            'navbar' => 'Navbar',
            'map_id' => 'Map ID',
            'map_data' => 'Map Data',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    public function getSpecial() {
        return $this->hasOne(CmsSpecial::class, ['id' => 'special_id']);
    }

    public function getSpecialId() {
        return $this->special_id;
    }

    /**
     * {@inheritdoc}
     */
    public function getSpecialArticles() {
        return $this->hasMany(CmsSpecialArticle::class, ['special_channel_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getArticles() {
        return $this->hasMany(CmsArticle::class, ['id' => 'article_id'])->via('specialArticles');
    }

    public function getThumbUrl() {
        return Url::attach($this->thumb);
    }

    public function getWeblink() {
        $data = $this->map_data ? unserialize($this->map_data) : [];
        if (!$this->reserved) {
            $data = [
                'id' => $this->id
            ];
        }
        $data['_title'] = $this->name;
        return Url::weblink($this->map_id, $data);
    }

    public function fields() {
        return [
            'id',
            'name' => 'abbr',
            'title' => 'name',
            'thumb' => 'thumbUrl',
            'description',
            'weblink',
        ];
    }

}
